package com.fy.controller;

import com.fy.ann.MyLog;
import com.fy.pojo.*;
import com.fy.service.MainService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("main")
public class MainController {
    @Autowired
    MainService mainService;

    @RequestMapping("list")
    public String list(Main main, @RequestParam(defaultValue = "1") Integer pageNum, @RequestParam(defaultValue = "3") Integer pageSize, Model model){
        PageInfo<Main> list = mainService.list(main, pageNum, pageSize);
        model.addAttribute("pg",list);
        model.addAttribute("main",main);
        return "list";
    }
    @ResponseBody
    @RequestMapping("getBuilt")
    public List<Built> getBuilt(){
        return mainService.getBuilt();
    }
    @ResponseBody
    @RequestMapping("getType")
    public List<Type> getType(){
        return mainService.getType();
    }
    @ResponseBody
    @RequestMapping("getGround")
    public List<Ground> getGround(){
        return mainService.getGround();
    }
    @RequestMapping("toAdd")
    public String toAdd(){
        return "add";
    }

    @MyLog
    @ResponseBody
    @RequestMapping("add")
    public Map<String,String> add(Main main){

        HashMap<String,String> map=new HashMap<>();
        map.put("error","");

        if(StringUtils.isEmpty(main.getBid())){
            map.put("error","场馆非空");
            return map;
        }
        if(StringUtils.isEmpty(main.getTid())){
            map.put("error","项目类型非空");
            return map;
        }
        if(StringUtils.isEmpty(main.getGid())){
            map.put("error","场馆场地非空");
            return map;
        }
        List<Prices> pricesList = main.getPricesList();
        if(pricesList==null||pricesList.size()==0){
            map.put("error","场馆价格设置至少有一条");
            return map;
        }
        for (Prices prices : pricesList) {
            if(StringUtils.isEmpty(prices.getStartTime())||StringUtils.isEmpty(prices.getEndTime())||StringUtils.isEmpty(prices.getPrice())){
                map.put("error","场馆价格设置信息不能为空");
                return map;
            }
        }
        mainService.addMain(main);
        map.put("success","保存成功");
        return map;
    }

    @RequestMapping("toEcharts")
    public String toEcharts(){
        return "echarts";
    }
    @ResponseBody
    @RequestMapping("echarts")
    public List<Main> echarts(){
        return mainService.count();
    }

}
